package com.umerclass.db;

import android.content.Context;
import android.database.SQLException;
import com.j256.ormlite.dao.Dao;
import com.umerclass.bean.user.UserInfo;
import com.umerclass.frame.ormlite.DataBaseHelper;
import java.util.List;

/**
 * Created by jack
 */

public class UserDao {
    private Context context;
    private Dao<UserInfo, Integer> userDao;
    private DataBaseHelper helper;

    public UserDao(Context context) {
        this.context = context;
        try {
            helper = DataBaseHelper.getHelper(context);
            userDao = helper.getDao(UserInfo.class);
        } catch (SQLException e)
        {
            e.printStackTrace();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新用户
     * @param user
     */
    public void update(UserInfo user){
        try {
            userDao.update(user);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     * @param user
     */
    public void add(UserInfo user) {
        try {
            userDao.create(user);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一个用户
     * @param user
     */
    public void delete(UserInfo user) {
        try {
            userDao.delete(user);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 删除所有的用户信息
     */
    public void deleteAll(){
        try {
            List<UserInfo> infos = userDao.queryForAll();
            if(infos!=null && infos.size()>0){
                for(int i =0;i<infos.size();i++){
                    delete(infos.get(i));
                }
            }
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取用户数
     * @return
     */
    public int getUserSize(){
        try {
            List<UserInfo> infos = userDao.queryForAll();
            if(infos!=null && infos.size()>0){
                return  infos.size();
            }else {
                return 0;
            }
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 查询一条记录
     * @param id
     * @return
     */
    public UserInfo queryForId(int id) {
        UserInfo userInfo = null;
        try {

            userInfo = userDao.queryForId(id);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        return userInfo;
    }

    /**
     * 查找第一个用户
     */
    public UserInfo query(){
        List<UserInfo> infos = null;
        try {
            infos = userDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        if(infos!=null && infos.size()>0){
            return infos.get(0);
        }else {
            return null;
        }

    }

    /**
     * 查找所有用户
     * @return
     */
    public List<UserInfo> queryAll() {
        List<UserInfo> infos = null;
        try {
            infos = userDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        if(infos!=null && infos.size()>0){
            return infos;
        }else {
            return null;
        }
    }

    //...other operations


}